#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <errno.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
    int sockfd;
    int ret;
    int i;
    char buf[1600];
    struct sockaddr_in src;
    struct sockaddr_in dst;
    
    sockfd = socket(PF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
        perror("sockfd < 0 \n");

    memset(&src, 0, sizeof(src));
    src.sin_family = AF_INET;
/*    src.sin_addr = INADDR_ANY; */
    ret = bind(sockfd, (struct sockaddr *)&src, sizeof(struct sockaddr_in));
    if (ret != 0)
        perror("bind ret < 0 \n");

    memset(&dst, 0, sizeof(dst));
    dst.sin_family = AF_INET;
    ret = inet_aton(argv[1], &dst.sin_addr);;
    if (ret == 0)
        perror("inet_aton == 0 \n");        
    else {
        printf("addr = %08x\n", dst.sin_addr.s_addr);
    }
    dst.sin_port = htons(2000);
/*
    ret = connect(sockfd, (struct sockaddr *)&src, sizeof(struct sockaddr_in));
    if (ret != 0)
        perror("connect ret < 0 \n");
*/
    for (i = 0; i < sizeof(buf); ++i) {
        buf[i] = (i % 20) + 'a';
    }
    ret = sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr *)&dst, sizeof(struct sockaddr_in));
    printf("send to ret = %d, err = %d\n", ret, errno);    

    ret = send(sockfd, buf, sizeof(buf), 0);
    printf("send ret = %d, err = %d\n", ret, errno);


    return (0);
}
